<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      :disabled="message === ''"
      class="post-btn"
      @click="onAddComment"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from '@/api/comments.js';
export default {
  name: 'CommentPost',
  components: {},
  inject: {
    articleId: {
      type: [Object, Number, String],
      defalut: null,
    },
  },
  props: {
    target: {
      type: [Object, Number, String],
      required: true,
    },
    addComment: {
      type: String,
      defalut: 'a',
    },
  },
  data() {
    return {
      message: '',
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onAddComment() {
      // addComment({
      //   target: this.target,
      //   content: this.message,
      //   art_id: null,
      // })
      //   .then((res) => {
      //     console.log(res);
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });

      this.$toast.loading({
        message: '发布中...',
        forbidClick: true, // 禁用背景点击
        duration: 0, // 持续时间，默认是2000, 如果为 0 则持续展示
      });
      try {
        const { data } = await addComment({
          target: this.target.toString(),
          content: this.message,
          art_id: this.addComment === 'c' ? this.articleId.toString() : null,
        });
        this.message = '';
        this.$emit('post-success', data.data);
        this.$toast.success('发布成功');
      } catch (err) {
        this.$toast.fail('发布失败');
      }
    },
  },
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
